/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assisisw.smartcal.webuisubsystem.managedbean;

import assisisw.smartcal.entitysubsystem.Event;
import assisisw.smartcal.entitysubsystem.ICalendarController;
import assisisw.smartcal.entitysubsystem.IEventController;
import assisisw.smartcal.entitysubsystem.INotificationController;
import assisisw.smartcal.entitysubsystem.Notification;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import static javax.faces.context.FacesContext.getCurrentInstance;
import javax.inject.Named;

/**
 *
 * @author Francesco
 */
@ManagedBean ()
@Named ( value = "notificationSectionBean" )
@RequestScoped ()
public class NotificationSectionBean implements Serializable
{
    
    // Static Fields
    
    /***/
    private static final String GOT_NOTIFICATION_MESSAGE = "Got it !" ;
    
    /***/
    private static final String REMIND_NOTIFICATION_MESSAGE = "Remind !" ;
    
    /***/
    private static final String ACCEPT_NOTIFICATION_MESSAGE = "Accept" ;
    
    /***/
    private static final String DECLINE_NOTIFICATION_MESSAGE = "Decline" ;
        
    // Instance Fields
    
    @EJB ()
    private ICalendarController calendarController ;
    
    /***/
    @EJB ()
    private IEventController eventController ;
    
    /***/
    @EJB ()
    private INotificationController notificationController ;
    
    // Instance Methods
    
    /***/
    public NotificationSectionBean () {}
          
    /**
     * @param notification
     * @return
     */
    public String generateNotificationTitle ( Notification notification ) 
    {
        String res ;
        switch ( notification.getType () ) 
        {
            case INVITATION :
                res = "Invitation : " + notification.getGeneratingEvent().getName () ;
            break ;
            case WEATHER :
                res = "Weather Alert : " + notification.getGeneratingEvent().getName () ;
            break ;
            case REMINDER :
                res = notification.getGeneratingEvent().getName () + " " + notification.getGeneratingEvent().getIniDate () ;
            break ;
            case SHARING :
                res = "Calendar shared with you !" + notification.getMessage();    // name of the one that shared the calendar with you
            break ;
            default :
                throw new IllegalArgumentException () ;
                
        }
        return res ;
    }
    
    /**
     * @param n
     * @return
     */
    public String generateNotificationContent ( Notification n ) 
    {
        String res ;
        Event genEvent ;
        genEvent = n.getGeneratingEvent () ;
        switch ( n.getType() ) 
        {
            case INVITATION :
                res = genEvent.getOwningCalendar().getOwner () + " has invited you to " + genEvent.getName () + " at " + genEvent.getPlace () + " from " + genEvent.getIniDate () + " to " + genEvent.getEndDate () ;
            break ;
            case WEATHER :
                res = "The weather for " + genEvent.getName() + " ( at " + genEvent.getPlace() + " " + genEvent.getIniDate () + " ) will be " ; // inserire tempo
            break ;
            case REMINDER :
                res = "Remember " + genEvent.getName () + " at " + genEvent.getPlace () + " from " + genEvent.getIniDate () + " to " + genEvent.getEndDate () ;
            break ;
            case SHARING :
                res = /* nome di quello che ti ha condiviso */ " has shared his 'nome calendario' with you";  
            break ;
            default :
                throw new IllegalArgumentException () ;
        }
        return res ;
    }
        
    /**
     * @param notification
     * @return
     */
    public String getLeftButtonText ( Notification notification ) 
    {
        switch ( notification.getType () )
        {
            case INVITATION :
            case SHARING :
                return ACCEPT_NOTIFICATION_MESSAGE ;
            case REMINDER :
            case WEATHER :
                return GOT_NOTIFICATION_MESSAGE ;
            default :
                throw new IllegalArgumentException () ;
        }
    }
    
    /**
     * @param n
     * @return 
     */
    public String getRightButtonText ( Notification n ) 
    {
        switch ( n.getType () )
        {
            case INVITATION :
            case SHARING :
                return DECLINE_NOTIFICATION_MESSAGE ;
            case REMINDER :
            case WEATHER :
                return REMIND_NOTIFICATION_MESSAGE ;
            default :
                throw new IllegalArgumentException () ;
        }
    }
    
    /**
     * @param notification
     */
    public void executeLeftAction ( Notification notification ) 
    {
        switch ( notification.getType () ) 
        {
            case REMINDER : // got it the reminder
                notificationController.markAsRead(notification); 
                getDashboardBean().onNotificationRemoved(notification); 
            break ;
            case SHARING :  // accept the calendar sharing
                notificationController.markAsRead(notification); 
                getDashboardBean().onNotificationRemoved(notification); 
            break ;
            case INVITATION :   //  accept the invitation 
                notificationController.markAsRead(notification); 
                getDashboardBean().onNotificationRemoved(notification); 
            break ;
            case WEATHER :  // got it the reminder
                notificationController.markAsRead(notification); 
                getDashboardBean().onNotificationRemoved(notification); 
            break ;
            default :
                throw new IllegalArgumentException () ;
        }
    }
    
    /**
     * @param notification
     */
    public void executeRightAction ( Notification notification ) 
    {
        switch ( notification.getType () ) 
        {
            case REMINDER : // dismiss the reminder
                getDashboardBean().onNotificationRemoved(notification); 
            break ;
            case SHARING :  // refuse the sharing
                // not implemented yet
                notificationController.markAsRead(notification); 
                getDashboardBean().onNotificationRemoved(notification); 
            break ;
            case INVITATION :   //  refuse the invitation
                eventController.disInvite ( notification.getGeneratingEvent() , getDashboardBean().getManagedUser() ) ;
                notificationController.markAsRead(notification); 
                getDashboardBean().onNotificationRemoved(notification); 
            break ;
            case WEATHER :  // dismiss the reminder
                getDashboardBean().onNotificationRemoved(notification); 
            break ;
            default :
                throw new IllegalArgumentException () ;
        }
    }
    
     /**
     * @return
     */
    protected DashboardBean getDashboardBean () 
    {
        DashboardBean dashboardBean ;
        dashboardBean = getCurrentInstance().getApplication().evaluateExpressionGet(FacesContext.getCurrentInstance(), "#{dashboardBean}",DashboardBean.class);
        return dashboardBean ;
    }
    
}
